package app.itgungnir.compose.wan.business.main.square

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import androidx.paging.compose.collectAsLazyPagingItems
import app.itgungnir.compose.wan.Routes
import app.itgungnir.compose.wan.UserInfo
import app.itgungnir.compose.wan.widgets.*

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun SquareScreen(navController: NavController = rememberNavController()) {
    val viewModel = viewModel<SquareViewModel>()
    // UI
    WanCustomScaffold(
        topBar = {
            SquareTopBar(
                navController = navController,
                hasLoginState = viewModel.hasLoginFlow.collectAsState(initial = false),
                userState = viewModel.userInfoFlow.collectAsState(initial = null)
            )
        }
    ) {
        val pagingItems = viewModel.squareDataFlow.collectAsLazyPagingItems()
        WanPagingGrid(
            pagingItems = pagingItems,
            contentPadding = PaddingValues(start = 10.dp, top = 5.dp)
        ) {
            items(pagingItems.itemCount) { index ->
                pagingItems[index]?.let { square ->
                    SquareItemUI(
                        item = square,
                        onAuthorClick = {
                            navToUserSquareScreen(
                                navController = navController,
                                userId = it.userId,
                                userName = it.shareUser
                            )
                        },
                        onArticleClick = {}
                    )
                }
            }
        }
    }
}

@Composable
private fun SquareTopBar(
    navController: NavController = rememberNavController(),
    hasLoginState: State<Boolean>,
    userState: State<UserInfo?>
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .background(color = MaterialTheme.colors.primary)
            .padding(horizontal = 15.dp, vertical = 10.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        val userId = userState.value?.id
        WanAvatar(
            modifier = Modifier.size(40.dp),
            userId = if (hasLoginState.value) userId else null
        )
        Spacer(modifier = Modifier.width(10.dp))
        WanHeader(
            modifier = Modifier.weight(1F),
            content = if (hasLoginState.value) userState.value?.name ?: "请先登录" else "请先登录",
            color = MaterialTheme.colors.onPrimary
        )
        WanTextButton(text = "进入我的主页", color = MaterialTheme.colors.secondary) {
            when (hasLoginState.value) {
                true -> navToUserSquareScreen(
                    navController = navController,
                    userId = userId,
                    userName = userState.value?.name
                )
                else -> navController.navigate(route = Routes.LOGIN)
            }
        }
    }
}

private fun navToUserSquareScreen(navController: NavController, userId: Long? = null, userName: String? = null) {
    userId?.takeIf { it > 0L }?.let { id ->
        userName?.takeIf { it.isNotEmpty() }?.let { name ->
            navController.navigate(Routes.UserSquare.route(id, name))
        }
    }
}
